/*
 * SaveListAsAction.java
 *
 * Copyright 2007, 2008 Tiberiumlabs
 *
 * This file is part of Tiberiumlabs Lailaps.
 *
 * Tiberiumlabs Lailaps is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Tiberiumlabs Lailaps is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.tiberiumlabs.lailaps.actions.list;

import java.awt.event.ActionEvent;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.tiberiumlabs.lailaps.actions.AbstractResourcedAction;
import org.tiberiumlabs.lailaps.actions.ActionFactory;
import org.tiberiumlabs.lailaps.gui.GUIBuilder;
import org.tiberiumlabs.lailaps.gui.elements.ExceptionDialog;
import org.tiberiumlabs.lailaps.list.ListStorage;
import org.tiberiumlabs.lailaps.resources.ImageLoader;
import org.tiberiumlabs.lailaps.settings.Settings;

/**
 *
 * @author <a href="paranoid.tiberiumlabs@gmail.com">Paranoid</a>
 */
public class SaveListAsAction extends AbstractResourcedAction implements Runnable {
    
    // TODO add resources to this string
    private static final String FILEDIALOG_TITLE = "Choose filename for your list...";
    
    /** Creates a new instance of SaveListAsAction */
    public SaveListAsAction() {
        putValue(NAME, bundle.getString("list.saveas.name"));
        putValue(SHORT_DESCRIPTION, bundle.getString("list.saveas.short"));
        putValue(LONG_DESCRIPTION, bundle.getString("list.saveas.long"));
        putValue(LARGE_ICON_KEY, ImageLoader.getSaveAsIcon());
        putValue(SMALL_ICON, ImageLoader.getMenuSaveAsIcon());
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        ActionFactory.execute(this);
    }

    @Override
    public void run() {
        try {
            JFileChooser fc = new JFileChooser(Settings.getApplicationSettings().LAILAPS_HOME);
            fc.setDialogTitle(FILEDIALOG_TITLE);
            fc.setDialogType(JFileChooser.SAVE_DIALOG);
            fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
            
            //FileNameExtensionFilter xmlFilter = new FileNameExtensionFilter("XML files (in Lailaps format)", "xml");
            //FileNameExtensionFilter gzFilter = new FileNameExtensionFilter("XML in GZ archive (usually faster than plain XML)", "gz");
            FileFilter plainFilter = new FileFilter() {
                
                @Override
                public boolean accept(File f) {
                    return true;
                }
                
                @Override
                public String getDescription() {
                    return "Plain text files (just list of urls)";
                }
            };
            
            //fc.setFileFilter(gzFilter);
            fc.setFileFilter(plainFilter);
            //fc.addChoosableFileFilter(xmlFilter);
            fc.setAcceptAllFileFilterUsed(false);
            
            GUIBuilder guiBuilder = mediator.getGuiBuilder();
            int res = fc.showSaveDialog(guiBuilder.isMainWindowVisible() ? guiBuilder.getMainView() : null); // TODO debug if parent works
            if (res == JFileChooser.APPROVE_OPTION) {
                File selectedFile = fc.getSelectedFile();
                FileFilter fileFilter = fc.getFileFilter();
//                if (fileFilter == gzFilter) {
//                    //ListSaver.saveGzList(selectedFile.getCanonicalPath());
//                }
//                if (fileFilter == xmlFilter) {
//                    //ListSaver.saveXmlList(selectedFile.getCanonicalPath());
//                }
                if (fileFilter == plainFilter) {
                    new ListStorage().savePlainTextList(selectedFile);
                }
            }
        } catch (Exception ex) {
            new ExceptionDialog(ex);
        }
    }
    
}
